package com.fancy.learncenter.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.VitaDetailDataBean;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;
import com.fancy.learncenter.view.PopupWindowEducationType;
import com.superservice.lya.R;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EducationExperienceActivity extends BaseActivity {


    @Bind(R.id.schoolName)
    EditText schoolName;
    @Bind(R.id.start_time)
    TextView startTime;
    @Bind(R.id.end_time)
    TextView endTime;
    @Bind(R.id.school_major)
    EditText schoolMajor;
    @Bind(R.id.education_tv)
    TextView educationTv;
    @Bind(R.id.is_all)
    ImageView isAll;
    @Bind(R.id.no_all)
    ImageView noAll;


    TimePickerView pvTime;
    TimePickerView edTime;
    @Bind(R.id.rootView)
    LinearLayout rootView;


    String resumeId;

    VitaDetailDataBean.EduListBean eduListBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_education_experience, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
        initToolbar("教育经历");

        resumeId = getIntent().getStringExtra("resumeId");
        eduListBean = getIntent().getParcelableExtra("EduListBean");

        if (eduListBean != null) {
            schoolName.setText(eduListBean.getSchool_name());
            startTime.setText(eduListBean.getStart_time());
            endTime.setText(eduListBean.getEnd_time());
            schoolMajor.setText(eduListBean.getProfession());


            if (eduListBean.getIs_formal().equals("1")) {
                isFormal = "1";
                isAll.setImageResource(R.mipmap.bg_xuanzhong);
                noAll.setImageResource(R.mipmap.bg_moren);
            } else {
                isFormal = "0";
                noAll.setImageResource(R.mipmap.bg_xuanzhong);
                isAll.setImageResource(R.mipmap.bg_moren);
            }

            educationId = eduListBean.getDiploma();
        }

        educationType = new PopupWindowEducationType(this, rootView);
        educationType.setClickCallBack(new PopupWindowEducationType.ClickCallBack() {
            @Override
            public void cancleCallBack() {

            }

            @Override
            public void selecCallBack(String content, String id) {
                educationTv.setText(content);
                educationId = id;
            }
        });

        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
                startTime.setText(Utils.getTime(date));
            }
        })
                .setType(new boolean[]{true, true, true, false, false, false})
                .build();
        edTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
                endTime.setText(Utils.getTime(date));
            }
        })
                .setType(new boolean[]{true, true, true, false, false, false})
                .build();
    }

    PopupWindowEducationType educationType;

    @OnClick({R.id.start_time, R.id.end_time, R.id.submit, R.id.education_tv, R.id.is_all, R.id.no_all})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.start_time:
                pvTime.show();
                break;
            case R.id.end_time:
                edTime.show();
                break;
            case R.id.education_tv:
                educationType.showPopupWindow();
                break;
            case R.id.is_all:
                isFormal = "1";
                isAll.setImageResource(R.mipmap.bg_xuanzhong);
                noAll.setImageResource(R.mipmap.bg_moren);
                break;
            case R.id.no_all:
                isFormal = "0";
                noAll.setImageResource(R.mipmap.bg_xuanzhong);
                isAll.setImageResource(R.mipmap.bg_moren);
                break;
            case R.id.submit:

                editPersonVita();
                break;
        }
    }

    String educationId = "";
    String isFormal = "1";

    private void editPersonVita() {
        Map map = new HashMap();

        map.put("schoolName", schoolName.getText().toString());

        map.put("profession", schoolMajor.getText().toString());

        map.put("startTime", startTime.getText().toString());

        map.put("endTime", endTime.getText().toString());

        map.put("resumeId", resumeId);

        map.put("diploma", educationId);

        map.put("isFormal", isFormal);

        if (eduListBean != null) {

            map.put("eduId", eduListBean.getId());
        }

        HttpMehtod.getInstance().doEduExperience(map, new IdeaObserver<BaseDataBean>(this) {

            @Override
            public void onSuccess(BaseDataBean listBaseDataBean) {
                ToastUtil.show("资料已更新");
                finish();
            }

            @Override
            public void onFail(int errorCode) {

            }
        });
    }


}
